<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="dao.QueryPlaces"%>
<%@page import="dao.QueryReservations"%>
<%@page import="dao.Entity.Reservations"%>
<%@page import="java.util.ArrayList"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="shortcut icon"  href="image/icon6.ico" type="image/x-icon" /> 
        <link href="css/allcss.css" rel="stylesheet" type="text/css"/>
        <title>PlaceTime</title>
        <script src="./script/jquery.js" type="text/javascript"></script>
    </head>
    <body>
        <div id="aside">
            <img id="icon" src="./image/icon6.png">
            <div id="p1">管理员界面</div>    
        </div>
        <div id="main">
            <%
                Reservations res = null;
                String param = request.getParameter("placetimenumber");
                if (param != null) {
                    res = QueryReservations.getResult(3, param);
                    out.println("<div id=number value='" + param + "'>时间号：" + param + "</div>");
                    for (int i = 0; i < res.reservations.size(); i++) {
                        String color = "red";
                        switch (res.reservations.get(i).AppointmentCheckStatus) {
                            case 1:
                                color = "green";
                                break;
                            case 2:
                                color = "yellow";
                                break;
                            case 3:
                                color = "cancel";
                                break;
                        }
                        int no = res.reservations.get(i).AppointmentNumber;
                        out.println("<div id='" + no + "' class='reservations'><div class='reservation'>预约号：[" + no
                                + "] 学生号：[" + res.reservations.get(i).StudentAccount + "] 预约时间：[" + res.reservations.get(i).AppointmentTime
                                + "]  地点：[" + res.reservations.get(i).PlacePosition + "] </div><div class='status'> 审核状态：</div><img class='status_img' src='./image/" + color + ".png'/></div>");
                    }
                }
            %>
            <div id="line"></div>
            <button id="updatePlaceTimeStatus_pass_button" class="button4">开放</button>
            <button id="updatePlaceTimeStatus_unpass_button" class="button4">占用</button>
            <button id="updatePlaceTimeStatus_del_button" class="button4">删除</button>
            <button id="updatePlaceTimeStatus_return_button"  class="button4" onclick="add(event)" >返回</button>
        </div>
        <script>
            const urlParams = new URLSearchParams(window.location.search);
            var placeNumber = urlParams.get('placenumber');
            $("#updatePlaceTimeStatus_pass_button").click(
                    function () {
                        $.get("./Servlet/updatePlaceTimeStatusServlet?placetimenumber=" + $("#number").attr("value") + "&type=1", function (data, status) {
                            if (status) {
                                if (data !== "0")
                                    alert("成功");
                                else
                                    alert("失败");
                            } else {
                                alert("无法连接服务器");
                            }
                        }
                        );
                    }
            );
            $("#updatePlaceTimeStatus_unpass_button").click(
                    function () {
                        $.get("./Servlet/updatePlaceTimeStatusServlet?placetimenumber=" + $("#number").attr("value") + "&type=0", function (data, status) {
                            if (status) {
                                if (data !== "0")
                                    alert("成功");
                                else
                                    alert("失败");
                            } else {
                                alert("无法连接服务器");
                            }
                        }
                        );
                    }
            );
            $("#updatePlaceTimeStatus_del_button").click(
                    function () {
                        alert($("#number").attr("value"));
                        $.get("./Servlet/updatePlaceTimeStatusServlet?placetimenumber=" + $("#number").attr("value") + "&type=2", function (data, status) {
                            if (status) {
                                if (data !== "0")
                                    alert("成功");
                                else
                                    alert("失败");
                            } else {
                                alert("无法连接服务器");
                            }
                        }
                        );
                    }
            );
            function add(event) {
                window.location.href = "updatePlace.jsp?placenumber=" + placeNumber; //This is a possibility                             
            }
            ;
        </script>        
    </body>
</html>
